<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片页 -->
    <el-card class="box-card">
      <!-- 搜索栏 -->
      <div>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-input placeholder="请输入内容" v-model="goodQuery" clearable @clear="clearSearch">
              <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
            </el-input>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="addGood">添加商品</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 用户信息列表 -->
      <el-table :data="queryData.goods" stripe border>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name" width="500px"></el-table-column>
        <el-table-column label="商品价格" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间" prop="add_time"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" @click="editGoods(scope.row.goods_id)" size="mini">修改
            </el-button>
            <el-button type="danger" icon="el-icon-delete" @click="handleDeleteGoods(scope.row.goods_id)" size="mini">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="parseInt(queryData.pagenum)" :page-sizes="[10, 20, 30, 40]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="queryData.total">
      </el-pagination>
    </el-card>
  </div>
</template>
<style lang="less" scoped>
  .el-table {
    margin-top: 30px;
  }

  .el-breadcrumb {
    margin-bottom: 20px;
  }

  .el-pagination {
    margin-top: 20px;
  }

</style>
<script>
  export default {
    data() {
      return {
        goodQuery: '',
        queryData: [],
        goodsData: [],
        queryInfo: {
          query: '',
          pagenum: 1,
          pagesize: 10
        },
      }
    },
    methods: {
      async getGoodInfo() {
        const {
          data: res
        } = await this.$http.get('/goods', {
          params: this.queryInfo
        })
        if (res.meta.status == 200) {
          this.queryData = res.data;
          this.queryData.goods.forEach(element => {
            element.add_time = this.timestampToTime(element.add_time);
          });
          //   this.$message.success(res.meta.msg);
        } else {
          this.$message.error(res.meta.msg);
        }
      },
      // 10位时间戳秒转换为标准时间 10位时间戳乘以1000
      timestampToTime(timestamp) {
        var date = new Date(timestamp * 1000)
        var Y = date.getFullYear() + '-'
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
        const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
        const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
        const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
        const s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
        return Y + M + D + h + m + s
      },
      editGoods(id) {
        console.log(id);
        this.$router.push(`/addGood?id=${id}`);
      },
      handleDeleteGoods(id) {
        console.log(id);
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          const {
            data: response
          } = await this.$http.delete(`/goods/${id}`, );
          if (response.meta.status == 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getGoodInfo();
          } else {
            this.$message({
              type: 'error',
              message: response.meta.msg
            });

          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleSizeChange(pageSize) {
        this.queryInfo.pagesize = pageSize;
        this.getGoodInfo();
      },
      handleCurrentChange(page) {
        this.queryInfo.pagenum = page;
        this.getGoodInfo();
      },
      search() {
        this.queryInfo.query = this.goodQuery;
        this.getGoodInfo();
      },
      clearSearch() {
        this.queryInfo.query = '';
        this.getGoodInfo();
      },
      addGood() {
        this.$router.push('/addGood');
      }

    },
    created() {
      this.getGoodInfo();
    }
  }

</script>
